﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JelleDruyts.Mollom.Client.Test
{
    [TestClass]
    public class XmlRpcFaultExceptionTest
    {
        [TestMethod]
        public void ConstructorShouldInitializeProperties()
        {
            XmlRpcFaultException target = new XmlRpcFaultException("message", "faultstring", 99);
            Assert.AreEqual<string>("message", target.Message);
            Assert.AreEqual<string>("faultstring", target.FaultString);
            Assert.AreEqual<int>(99, target.FaultCode);

            // Call other overloads too.
            target = new XmlRpcFaultException();
            Assert.IsNotNull(target.Message);
            Assert.IsNull(target.InnerException);

            target = new XmlRpcFaultException("message");
            Assert.AreEqual<string>("message", target.Message);
            Assert.IsNull(target.InnerException);

            Exception innerException = new Exception();
            target = new XmlRpcFaultException("message", innerException);
            Assert.AreEqual<string>("message", target.Message);
            Assert.AreSame(innerException, target.InnerException);
        }

        [TestMethod]
        public void DeserializedExceptionShouldEqualOriginal()
        {
            XmlRpcFaultException target = new XmlRpcFaultException("message", "faultstring", 99);

            SoapFormatter formatter = new SoapFormatter();
            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, target);
                stream.Flush();
                stream.Position = 0;
                object deserializedObject = formatter.Deserialize(stream);

                Assert.AreNotSame(target, deserializedObject);
                Assert.IsInstanceOfType(target, typeof(XmlRpcFaultException));
                XmlRpcFaultException deserializedException = (XmlRpcFaultException)deserializedObject;

                Assert.AreEqual<string>(target.Message, deserializedException.Message);
                Assert.AreEqual<string>(target.FaultString, deserializedException.FaultString);
                Assert.AreEqual<int>(target.FaultCode, deserializedException.FaultCode);
            }
        }
    }
}
